# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=twisted
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgver=22.10.0
pkgrel=2
pkgdesc='An asynchronous networking framework written in Python (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='https://twistedmatrix.com'
license=('MIT')
depends=(
    ${MINGW_PACKAGE_PREFIX}-python
    ${MINGW_PACKAGE_PREFIX}-python-appdirs
    ${MINGW_PACKAGE_PREFIX}-python-automat
    ${MINGW_PACKAGE_PREFIX}-python-bcrypt
    ${MINGW_PACKAGE_PREFIX}-python-constantly
    ${MINGW_PACKAGE_PREFIX}-python-cryptography
    ${MINGW_PACKAGE_PREFIX}-python-h2
    ${MINGW_PACKAGE_PREFIX}-python-hyperlink
    ${MINGW_PACKAGE_PREFIX}-python-idna
    ${MINGW_PACKAGE_PREFIX}-python-incremental
    ${MINGW_PACKAGE_PREFIX}-python-priority
    ${MINGW_PACKAGE_PREFIX}-python-pyasn1
    ${MINGW_PACKAGE_PREFIX}-python-pyopenssl
    ${MINGW_PACKAGE_PREFIX}-python-pyserial
    ${MINGW_PACKAGE_PREFIX}-python-pywin32
    ${MINGW_PACKAGE_PREFIX}-python-service_identity
    ${MINGW_PACKAGE_PREFIX}-python-twisted_iocpsupport
    ${MINGW_PACKAGE_PREFIX}-python-typing_extensions
    ${MINGW_PACKAGE_PREFIX}-python-zope.interface
)
makedepends=(${MINGW_PACKAGE_PREFIX}-python-setuptools
             ${MINGW_PACKAGE_PREFIX}-python-setuptools-scm)
source=(https://github.com/twisted/${_realname}/archive/${_realname}-${pkgver}.tar.gz)
sha256sums=('2a310f83278b54d8e8e2b9d804baf839835f80ab5b42e141a370cdc170dd7ddb')

prepare() {
  rm -rf python-build-${MSYSTEM} | true
  cp -r "twisted-${_realname}-${pkgver}" "python-build-${MSYSTEM}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    # Remove shebang line
    sed -e '1 { s/^#!.*$// }' -i "${_f}"
  done
}
